home *** CD-ROM | disk | FTP | other *** search
- /* g e t s
- *
- * Read a line from stdin. The line is read up until the next
- * newline character or EOF is encountered. The newline
- * character is not inserted into the buffer, but the buffer
- * is terminated with a null character. No checks are made
- * that the line will fit into the buffer. The function returns
- * a pointer to the buffer. If EOF is encountered before any
- * characters have been read, the NULL pointer is returned.
- *
- * Patchlevel 1.0
- *
- * Edit History:
- * 02-Sep-1989 Speed up by reading directly from buffer.
- */
-
- #include "stdiolib.h"
-
- /*LINTLIBRARY*/
-
- char *gets(buf)
-
- char *buf; /* input buffer */
-
- {
- int ch; /* next character */
- unsigned char *q; /* input buffer pointer */
- unsigned char *s; /* user's buffer pointer */
- unsigned int bytesleft; /* bytes left in current load */
-
- if (TESTFLAG(stdout, _IOLBF))
- (void) fflush(stdout);
-
- for (s = (unsigned char *) buf; ;*s++ = ch) {
- if ((bytesleft = BYTESINREADBUFFER(stdin)) != 0) {
- q = GETREADPTR(stdin);
- UNROLL_DO(fgetsbytes, bytesleft, if ((*s++ = *q++) == '\n') break);
- SETREADPTR(stdin, q);
- }
- if (bytesleft != 0) {
- *--s = 0;
- return buf;
- }
- *s = 0;
- if ((ch = getc(stdin)) == EOF)
- return s == (unsigned char *) buf ? NULL : buf;
- if (ch == '\n')
- return buf;
- }
- }
-